package com.apollow.oschina.base;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.apollow.oschina.interf.Const;

/**
 * 封装一些全局的操作：屏幕尺寸，主线程运行，toast，infate布局等
 * Created by bobo on 2017/1/3.
 */

public class Global {
    /** 上下文对象 */
    public static Context mContext;
    /** 屏幕宽度 */
    public static int mScreenWidth;
    /** 屏幕高度 */
    public static int mScreenHeigth;
    /** 屏幕密度 */
    public static float mDensity;

    /** 初始化上下文和屏幕参数 */
    public static void init(Context context){
        mContext = context;
        initScreenSize();
    }
    /** 初始化屏幕尺寸和密度 */
    private static void initScreenSize() {
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        mScreenWidth = dm.widthPixels;
        mScreenHeigth = dm.heightPixels;
        mDensity = dm.density;
    }
    /** dp转px */
    public static int dp2px(int dp) {
        return (int) (dp * mDensity + 0.5f);
    }

    public static Handler mHandler = new Handler(Looper.getMainLooper());

    /** 主线程运行 */
    public static void runOnUIThread(Runnable runnable){
        if (isMainThread()){// 当前为主线程
            runnable.run();
        }else {// 子线程
            mHandler.post(runnable);
        }
    }

    private static Toast mToast;
    /**
     * showToast,可以在子线程调用
     * @param msg
     */
    public static void showToast(final String msg){
        runOnUIThread(new Runnable() {
            @Override
            public void run() {
                if (mToast == null){
                    mToast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
                }else
                mToast.setText(msg);
                mToast.show();
            }
        });
    }
    /**
     * 解析布局文件生成对应的视图对象
     */
    public static View inflate(int layoutRes, ViewGroup parent){
        return LayoutInflater.from(mContext).inflate(layoutRes,parent,false);
    }
    //布局填充:
    public static View layoutInflate(int LayoutResId){

        return View.inflate(mContext,LayoutResId,null);
    }

    public static boolean isMainThread(){
        return Looper.myLooper()==Looper.getMainLooper();//轮训器;
    }

    // TODO: 2017/1/17 获取综合页面中已经读取的状态;
    //存储状态:
    public static void putReadedews(String id){
        SharedPreferences sp = Global.mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(Const.READ_ITEM_NEWS, id);
        edit.commit();
    }
    /**
     * 获取用户上一次使用的音频播放模式
     * @param defPlayMode 获取不到时指定的默认值
     * @return
     */
    public static String getReadedNews(String id){
        SharedPreferences sp = Global.mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
        return sp.getString(Const.READ_ITEM_NEWS,id);
    };
}
